package com.wjm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wjm.entity.Student;
import com.wjm.mapper.interf.StudentMapper;
import com.wjm.service.interf.StudentServiceInterface;

@Service("studentService")
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class) // 注明需要回滚
public class StudentServiceImpl implements StudentServiceInterface {

	private boolean result;

	@Autowired
	private StudentMapper studentMapper;

	@Override
	public boolean save(Student object) throws Exception {
		if (studentMapper.save(object) > 0) {
			result = true;
		} else {
			result = false;
		}
		return result;
	}

	@Override
	public boolean delete(int objectId) throws Exception {
		if (studentMapper.delete(objectId) > 0) {
			result = true;
		} else {
			result = false;
		}
		return result;
	}

	@Override
	public boolean update(Student object) throws Exception {
		if (studentMapper.update(object) > 0) {
			result = true;
		} else {
			result = false;
		}
		return result;
	}

	@Override
	public Student login(String stuNo, String stuPass) throws Exception {
		Student stu = studentMapper.selectByIdOrNo(0, stuNo);
		if (stu == null || !stu.getStuPass().equals(stuPass) || stu.getStuStatus() == 2) {
			stu = null;
		}
		return stu;
	}

	@Override
	public Student queryByIdOrNo(int stuId, String stuNo) throws Exception {
		Student stu = null;
		if (stuId > 0) {
			stu = studentMapper.selectByIdOrNo(stuId, null);
		} else if (stuNo != null && !stuNo.equals("")) {
			stu = studentMapper.selectByIdOrNo(0, stuNo);
		}
		return stu;
	}

	@Override
	public int queryTotal(int status) throws Exception {
		return studentMapper.selectTotal(status);
	}

	@Override
	public List<Student> queryByPage(int page, int number, int status) throws Exception {
		page = (page - 1) * number;
		return studentMapper.selectByPage(page, number, status);
	}

	@Override
	public boolean updateStatus(int stuId, int status) throws Exception {
		boolean result = false;
		if (studentMapper.updateStatus(stuId, status) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public boolean reset(int stuId) throws Exception {
		boolean result = false;
		if (studentMapper.reset(stuId) > 0) {
			result = true;
		}
		return result;
	}

}
